<?php

/**
* Email.class.php
*
* Register user
*
* @author     Blazej Zieba <b@zieba.info>
* @copyright  2007 konekto.com
* @version    CVS: $Id:$
*/
require_once("framework/AuthSingleton.php");
require_once("framework/config.php");
require_once("framework/PageModule.class.php");
require_once("HTML/QuickForm.php");
require_once("HTML/QuickForm/Renderer/Default.php");
require_once("HTML/Template/IT.php");


// {{{ uthRe

class EmailModule extends PageModule 
{
    // {{{ getContent()

    
    /**
    * getContent()
    *
    */

    function getContent()
    {
        
        trigger_error("ENTER EmailModule::getContent()", E_USER_NOTICE);

        $options = &PEAR::getStaticProperty('CONFIG_INI','TEMPLATES'); 
        $global = &PEAR::getStaticProperty('CONFIG_INI','GLOBAL');
    
        $form = new HTML_QuickForm("Email", 'post', rtrim($global['service_url'],'/').'/index.php?cmd=Email');

        $statusTpl = new HTML_Template_IT($options["templates_dir"]);
        $statusTpl->loadTemplatefile("status.tpl.xhtml",FALSE,FALSE);

        $form->addElement('text','email',_('Email znajomego:'));

        $text = sprintf(_('Zapraszam Cię do udziału w zabawie w polowanie na kody. Wejdź na stronę %s w swojej komórce.'),rtrim($global['service_url'],'/'));
        $form->addElement('static',null,_('Wiadomość:'),$text);
        $form->addElement('static',null,_('Nadawca:'),AuthSingleton::instance('AUTH_USER')->getUsername());
        
        $form->applyFilter('email', 'trim');
        $form->addRule('email', _('Podaj email.'), 'required');
        $form->addRule('email', _('Błędny format emaila'), 'email');
        $form->addElement('submit', 'Submit',_('Wyślij'));

        if ($form->validate()) 
        {
        
            $data = $form->getSubmitValues(true);

            $hdrs = array(
                          'From'    =>  $global['email'],
                          'Subject' => _('Zaproszenie od znajomego.')
                          );


            $params = array ( 'host' => $global['host'],
                              'auth'=>true,
                              'username'=>$global['username'],
                              'password'=>$global['password'],
                              'port'=>$global['port']);

            $mail =& Mail::factory('smtp',$params);
            if(PEAR::isError($mail))
            {
               trigger_error("Email::getContent() ".$mail->getMsg(), E_USER_ERROR); 
            }

            $err = $mail->send($data['email'], $hdrs, $text."\n\r--\n".AuthSingleton::instance('AUTH_USER')->getUsername());

            if(PEAR::isError($err))
            {
               trigger_error("Email::getContent() ".$err->getMessage(), E_USER_ERROR); 
            }

            $statusTpl->setVariable("STATUS",_("Wiadomość wysłana."));                                  
            return $statusTpl->get();

        }
        else
        {
 
            $renderer = new HTML_QuickForm_Renderer_Default($options);
            $renderer -> setFormTemplate(file_get_contents($options["templates_dir"]."form.tpl.xhtml"));
            $renderer -> setElementTemplate(file_get_contents($options["templates_dir"]."formElement.tpl.xhtml"));
            $renderer -> setRequiredNoteTemplate(file_get_contents($options["templates_dir"]."formRequiredNote.tpl.xhtml"));
            $form -> setRequiredNote(null); 
            $form->accept($renderer);
            $statusTpl->setVariable("STATUS",_('Wyślil zaproszenie do znajomego.'));
            return $statusTpl->get().$renderer->toHtml().$statusTpl->get(); 
        }




    }
    
    // }}}

}

// }}} 

?>
